var layer = null;
var form = null;
layui.use(['table', 'layer'], function () {
    table = layui.table;
    $ = layui.$;
    layer = layui.layer;
    renderTable();

    table.on('tool(user-table)', function (obj) {
        switch (obj.event) {
            case 'edit':
                layer.open({
                    title: false,
                    type: 2,
                    area: ['650px', '610px'],
                    content: ctx + 'sys/user-edit.html?userId=' + obj.data.userId ,
                    end: function () {
                        location.reload();
                    }
                })
                break;
                break;
            case 'del':
                layer.open({
                    content: '删除后不可恢复，是否确认删除'
                    ,btn: ['删除', '取消']
                    ,yes: function(index, layero){
                        axios.post(ctx + 'sys/user/delete', [obj.data.userId]).then(function (res) {
                            if (res.data.code == 0) {
                                layer.msg('删除成功');
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                location.reload();
                            } else {
                                parent.layer.msg(res.data.msg);
                            }

                        })
                    }
                    ,btn2: function(index, layero){

                    }
                    ,cancel: function(){
                        //右上角关闭回调
                    }
                });
                break;
        }
    });

    $('#user-add').click(function () {
        layer.open({
            title: false,
            type: 2,
            area: ['650px', '460px'],
            content: ctx + 'sys/user-add.html',
            end: function () {
                location.reload();
            }
        })
    })
    $('#user-search').click(function () {
        var keyword  = $('#username-input').val();
        renderTable(keyword);
    })
});

function renderTable(keyword) {
    url = ctx + 'sys/user/list'
    if(keyword) {
        url = ctx + 'sys/user/list?username=' + keyword
    }
    table.render({
        elem: '#user-table'
        , title: '用户表'
        , url: url
        , page: true
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'userId', title: '用户ID', fixed: 'left', sort: true, width: 90}
            , {field: 'username', title: '用户名', width: 120}
            , {field: 'realName', title: '真实姓名', width: 120}
            , {field: 'uuid', title: 'UUID', width: 200}
            , {
                field: 'roleList', title: '角色', width: 320, templet: function (res) {
                    var roleList = '';
                    res.roleList.forEach(function (role, index) {
                        if (index == res.roleList.length - 1) {
                            roleList += role.roleName;
                        } else {
                            roleList += role.roleName + ' | ';
                        }
                    });
                    return roleList;
                }
            }
            , {
                field: 'status', title: '状态', width: 90, templet: function (res) {
                    return res.status == 1 ? '<span class="layui-badge layui-bg-blue">正常</span>' : '<span class="layui-badge">禁用</span>'
                }
            }
            , {field: 'createTime', title: '创建时间'}
            , {fixed: 'right', title: '操作', toolbar: '#toolbar', width: 200}
        ]]
        , cellMinWidth: 120
    });
}